home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_contacshadow.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  2.3 KB  |  63 lines

  1. /* renamed shader to SIG2k_srf_contact to be consistent with RMR 
  2.  *    -- tal@SpamSucks_renderman.org
  3.  */
  4.  
  5. /*  
  6. **  
  7. ** Render a contact shadow based on depth data derived from a light  
  8. ** placed onto the surface which catches the contact shadow  
  9. **  
  10. ** by Rob Engle and Jim Berney  
  11. */  
  12.  
  13. surface 
  14. k3d_contactshadow (  
  15.     string shadowname = "";     /* the name of the texture file */  
  16.     float samples = 10;         /* how many samples to take per Z lookup */  
  17.     float influence = 1.0;      /* world space distance in which effect is visible */  
  18.     float gamma = 0.5;          /* controls ramp on of effect over distance */  
  19.     float maxdist = 10000;      /* how far is considered infinity */  
  20.     )
  21. {  
  22.     /* get a matrix which transforms from current space to the  
  23.        camera space used when rendering the shadow map */  
  24.     uniform matrix matNl;  
  25.     textureinfo(shadowname, "viewingmatrix", matNl);  
  26.     
  27.     /* get a matrix which transforms from current space to the  
  28.        screen space (-1..1) used when rendering the shadow map */  
  29.     uniform matrix matNP;  
  30.     textureinfo(shadowname, "projectionmatrix", matNP);  
  31.  
  32.     /* transform the ground plane point into texture coordinates  
  33.        needed to look up the point in the shadow map */  
  34.     point screenP = transform(matNP, P);  
  35.     float ss = (xcomp(screenP) + 1) * 0.5;  
  36.     float tt = (1 - ycomp(screenP)) * 0.5;  
  37.  
  38.     if (ss < 0 || ss > 1 || tt < 0 || tt > 1) {  
  39.     /* point being shaded is outside the region of the depth map */  
  40.     Ci = color(0);  
  41.     }  
  42.     else {  
  43.     /* get the distance from the shadow camera to the closest
  44.        object as recorded in the shadow map */
  45.     float mapdist = float texture(shadowname, ss, tt, "samples", samples);
  46.     
  47.     /* transform the point on the ground plane into the shadow
  48.        camera space in order to get the distance from the shadow
  49.        camera to the ground plane */
  50.     point cameraP = transform(matNl, P);  
  51.     
  52.     /* the difference between the two distances is used to calculate the  
  53.        contact shadow effect */  
  54.     float distance = mapdist - zcomp(cameraP);  
  55.     
  56.     distance = smoothstep(0, 1, distance/influence);  
  57.     distance = pow(distance, gamma);  
  58.  
  59.     /* convert into a color (white=shadow) */  
  60.     Ci = (1.0-distance);  
  61.     }  
  62. }
  63.